package kr.pe.dothome.yyar.myphone;

import java.util.Date;

import kr.pe.dothome.yyar.myphone.controller.C2DMDataSendingTask;
import kr.pe.dothome.yyar.myphone.controller.MissedMessageSendingTask;
import kr.pe.dothome.yyar.myphone.controller.Responser;
import kr.pe.dothome.yyar.myphone.domain.Request;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;


public class C2DMReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d("Registration", "Received C2DM Message");
		if (intent.getAction().equals(
				"com.google.android.c2dm.intent.REGISTRATION")) {
			handleRegistration(context, intent);
		} else if (intent.getAction().equals(
				"com.google.android.c2dm.intent.RECEIVE")) {
			handleMessage(context, intent);
		}
	}

	private void handleRegistration(Context context, Intent intent) {
		Log.d("Registration", "Regiration Handler Executed");
		
		String registrationId = intent.getStringExtra("registration_id");
		if (intent.getStringExtra("error") != null) {
			// Registration failed, should try again later.
			Log.d("Registration", "Registration Error Occurred");
		} else if (intent.getStringExtra("unregistered") != null) {
			// unregistration done, new messages from the authorized sender will
			// be rejected
			Log.d("Registration", "Registration Failed");
		} else if (registrationId != null) {
			// Send the registration ID to the 3rd party site that is sending
			// the messages.
			// This should be done in a separate thread.
			// When done, remember that all registration is done.

			TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
			String deviceId = telephonyManager.getDeviceId();
			String phoneNumber = telephonyManager.getLine1Number();
			new C2DMDataSendingTask().execute(registrationId, deviceId, phoneNumber);
			
			SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(context);
			preference.edit().putString("rid", registrationId).commit();
			
			Log.d("Registration", "Registration Id Sended");
			Toast.makeText(context, "Restration Id : " + registrationId, Toast.LENGTH_SHORT).show();
		}
	}

	private void handleMessage(Context context, Intent intent) {
		String rid = intent.getStringExtra("rid");
		
		String startDateString = intent.getStringExtra("startdate");
		String endDateString = intent.getStringExtra("enddate");
		long startDate = Long.parseLong(startDateString);
		long endDate = Long.parseLong(endDateString);
		
		String logMessage =  "C2DM Message RID : " + rid + "\nStart Date : " + new Date(startDate * 1000) + "\nEnd Date : " + new Date(endDate * 1000) + "\n";
		Toast.makeText(context, logMessage, Toast.LENGTH_LONG).show();
		
		Log.d("requestMessages", logMessage);
//		Log.d("requestMessages", "StartDateString:" + startDateString + ", EndDateString:" + endDateString);
		
		Responser resp = new Responser(context.getContentResolver(), startDate, endDate);
		Request req = resp.getRequestResponse(rid);
		
		new MissedMessageSendingTask(context).execute(req);
		
		Log.d("requestMessages", req.toString());	
	}
}
